package com.cs.auth.domain.service.impl;

import com.cs.auth.api.AuthUserClient;
import com.cs.auth.common.bos.AuthUserBO;
import com.cs.auth.common.entity.BaseResponse;
import com.cs.auth.common.entity.ErrorCode;
import com.cs.auth.common.utlis.ParseClientResultUtils;
import com.cs.auth.common.utlis.ThrowUtils;
import com.cs.auth.domain.service.AuthCaptchaDomainService;
import com.cs.auth.domain.service.AuthUserDomainService;
import com.cs.auth.infra.basic.entity.AuthUser;
import com.cs.auth.infra.basic.service.AuthUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AuthUserDomainServiceImpl implements AuthUserDomainService {

    @Resource
    private AuthCaptchaDomainService captchaDomainService;

    @Resource
    private AuthUserClient authUserClient;

    @Resource
    private ParseClientResultUtils parseClientResultUtils;

    /**
     * 用户注册
     */
    @Override
    public Boolean register(AuthUserBO authUserBO) {
        // 参数校验
        ThrowUtils.throwIf(authUserBO.getUserAccount() == null, ErrorCode.PARAMS_ERROR,"你账号呢？");
        ThrowUtils.throwIf(authUserBO.getUserAccount().length() != 12 && authUserBO.getUserAccount().length() != 8, ErrorCode.PARAMS_ERROR,"账号长度不符合要求");
        ThrowUtils.throwIf(authUserBO.getUserPassword() == null, ErrorCode.PARAMS_ERROR,"你密码呢？");
        ThrowUtils.throwIf(authUserBO.getUserPassword().length() >= 13 || authUserBO.getUserPassword().length() <= 6, ErrorCode.PARAMS_ERROR,"密码长度不符合要求");
        ThrowUtils.throwIf(authUserBO.getCheckPassword() == null, ErrorCode.PARAMS_ERROR,"你确认密码呢？");
        ThrowUtils.throwIf(!authUserBO.getUserPassword().equals(authUserBO.getCheckPassword()), ErrorCode.PARAMS_ERROR,"两次密码不一致");
        // 校验验证码
        String code = authUserBO.getAuthCaptchaBO().getCode();
        String uuid = authUserBO.getAuthCaptchaBO().getUuid();
        Boolean flag = captchaDomainService.checkCaptcha(code, uuid);
        ThrowUtils.throwIf(!flag, ErrorCode.PARAMS_ERROR,"验证码错误");
        // 调用feign方法
        BaseResponse<Boolean> res = authUserClient.register(authUserBO);
        // 校验feign方法返回值
        return parseClientResultUtils.parseClientResult(res);
    }

    @Override
    public AuthUserBO login(AuthUserBO authUserBO) {
        // 参数校验
        ThrowUtils.throwIf(authUserBO.getUserAccount() == null, ErrorCode.PARAMS_ERROR,"你账号呢？");
        ThrowUtils.throwIf(authUserBO.getUserAccount().length() != 12 && authUserBO.getUserAccount().length() != 8, ErrorCode.PARAMS_ERROR,"账号长度不符合要求");
        ThrowUtils.throwIf(authUserBO.getUserPassword() == null, ErrorCode.PARAMS_ERROR,"你密码呢？");
        ThrowUtils.throwIf(authUserBO.getUserPassword().length() >= 13 || authUserBO.getUserPassword().length() <= 6, ErrorCode.PARAMS_ERROR,"密码长度不符合要求");
        // 校验验证码
        String code = authUserBO.getAuthCaptchaBO().getCode();
        String uuid = authUserBO.getAuthCaptchaBO().getUuid();
        Boolean flag = captchaDomainService.checkCaptcha(code, uuid);
        ThrowUtils.throwIf(!flag, ErrorCode.PARAMS_ERROR,"验证码错误");
        // 调用feign方法
        BaseResponse<AuthUserBO> loginUser = authUserClient.login(authUserBO);
        // 校验feign方法返回值
        return parseClientResultUtils.parseClientResult(loginUser);
    }
}
